package cn.eng.org.controller;

import cn.eng.basic.annotation.PreAuthorize;
import cn.eng.basic.util.AjaxResult;
import cn.eng.basic.util.PageList;
import cn.eng.org.domain.Department;
import cn.eng.org.query.DepartmentQuery;
import cn.eng.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门接口
 */
@RestController //复合注解: ResponseBody+Controller 组合
@RequestMapping("/department")
@Api(value = "部门的api",description = "部门的所有crud操作")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 接口: 查询单个
     * @param id
     * @return
     */
    // get http://localhost:8080/department/1
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象,通过ID查询")
    @PreAuthorize(name = "查询单个部门",sn = "department:getOne") //value后面用来做权限校验
    public Department loadById(@PathVariable("id") Long id){
      return  departmentService.loadById(id);
    }

    /**
     * 接口:查询所有
     * @return
     */
    //get http://localhost:8080/department
    @GetMapping
    @ApiOperation(value = "查询部门所有对象")
    @PreAuthorize(name = "查询部门列表",sn = "department:getAll") //value后面用来做权限校验
    public List<Department> loadAll(){
        return departmentService.loadAll();
    }

    /**
     * 接口: 分页查询接口
     * @param departmentQuery
     * @return
     */
    //post http://localhost:8080/department 对象
    @PostMapping
    @ApiOperation(value = "分页查询")//@Department 从请求体里面获取 对象的数据 赋值给department
    @PreAuthorize(name = "部门列表",sn = "department:list") //value后面用来做权限校验
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    //delete http://localhost:8080/department/1
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除对象")//@PathVariable 从Url地址里面 获取id 并赋值给Long id
    @PreAuthorize(name = "删除部门",sn = "department:delete") //value后面用来做权限校验
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return new AjaxResult();
         } catch (Exception e) {
         e.printStackTrace();
         return new AjaxResult(false,"删除失败");
         }
    }

    /**
     * 接口: 添加接口和修改接口
     * @param department
     * @return
     */
    //put http://localhost:8080/department/ 对象
    @PutMapping                     //@Department 从请求体里面获取 对象的数据 赋值给department
    @ApiOperation(value = "添加对象或者修改对象")
    @PreAuthorize(name = "保存部门",sn = "department:save") //value后面用来做权限校验
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            //判断,如果传过来的对象获取id 为null 无id 执行添加业务
            if(department.getId()==null){
                departmentService.save(department);
            }else {
                //有id 执行修改业务
                departmentService.update(department);
            }
            return new AjaxResult();
         } catch (Exception e) {
         e.printStackTrace();
         return new AjaxResult(false,"操作失败");
         }
    }

    /**
     * 批量删除 接口
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除对象")//@PathVariable 从Url地址里面 获取id 并赋值给Long id
    @PreAuthorize(name = "批量删除部门",sn = "department:patchDel") //value后面用来做权限校验
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            departmentService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 查询上级部门级联树的接口
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "查询部门树")
    @PreAuthorize(name = "查询部门树",sn = "department:deptTree") //value后面用来做权限校验
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

}
